package com.potato.model1.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.potato.base.fragment.LazyFragment;
import com.potato.model1.R;
import com.potato.model1.adapter.HomeSubAdapter;
import com.potato.model1.bean.CategoryResult;
import com.potato.model1.contract.HomeSubContract;
import com.potato.model1.contract.HomeSubPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

public class HomeSubFragment extends LazyFragment<HomeSubPresenter> implements HomeSubContract.MainView {

    private HomeSubPresenter presenter;
    private String mType;
    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private int mPage=1;
    private HomeSubAdapter mSubAdapter;

    public static HomeSubFragment newInstance(String type){
        Bundle bundle =new Bundle();
        bundle.putString("type",type);
        HomeSubFragment fragment=new HomeSubFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public HomeSubPresenter loadPresenter() {
        return presenter =new HomeSubPresenter();
    }

    @Override
    public int layoutRes() {
        return R.layout.fragment_home_sub;
    }
    @Override
    public void initView(View view) {
        refreshLayout=view.findViewById(R.id.refreshLayout);
        recyclerView=view.findViewById(R.id.recyclerView);
        mType=getArguments().getString("type");
    }

    @Override
    public void initData() {
        getCategory(mType);
    }

    @Override
    public void initEvent() {
        super.initEvent();
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            mPage++;
            getCategory(mType);
        });
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPage=1;
            getCategory(mType);
        });
    }

    @Override
    public void showError(String msg) {
        Log.e("Tag","msg "+msg);
       refreshLayout.finishRefresh();
       refreshLayout.finishLoadMore();
    }

    public void getCategory(String mType){
        refreshLayout.autoRefresh();
        Log.e("Tag","mType "+mType);
        presenter.getDank(mType,10+"",mPage+"");
    }

    @Override
    public void showData(List<CategoryResult> results) {
        if (mSubAdapter==null){
            mSubAdapter=new HomeSubAdapter(results);
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerView.setAdapter(mSubAdapter);
            refreshLayout.finishRefresh();
        }else{
            if (mPage==1){
                mSubAdapter.setNewData(results);
                refreshLayout.finishRefresh();
            }
            else{
                mSubAdapter.addData(results);
                if (results.size()<10){
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }else{
                    refreshLayout.finishLoadMore();
                }
            }
        }
    }
}
